package com.xauto.pioneer.appuser.activity.mine.user;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.common.TakePhotoActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.Image;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.DataCheckUtil;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.util.TimeUtil;
import com.xauto.pioneer.appuser.util.ToastUtil;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;

//import static com.xauto.pioneer.appuser.R.id.tvDriverInfoTakePhoto;

/***
 * 驾驶证信息认证页面
 * @author Lemon Fri 24 Feb 17:45
 */
public class DriverInformationActivity extends BaseActivity {
    private static  String DRIVE_AUTH_STATUS = "001";
    @Bind(R.id.tvDriverInfoTakePhoto) TextView tvDriverInfoTakePhoto;
    @Bind(R.id.topBar) TopBar topBar;
    /** 驾驶证认证界面*/
    @Bind(R.id.layoutVerifyDriver) View layoutVerifyDriver;
    /** 显示-驾驶证*/
    @Bind(R.id.ivDriverInfo) ImageView ivDriverInfo;
    @Bind(R.id.tvDriverInfoNext) TextView tvDriverInfoNext;
    /** 驾驶证审核中界面*/
    @Bind(R.id.layoutPromptStatus) View layoutPromptStatus;
    /** 审核通过界面*/
    @Bind(R.id.layoutPromptSuccess) View layoutPromptSuccess;
    /** 审核通过证件照片界面*/
    @Bind(R.id.ivDriverSuccess) ImageView ivDriverSuccess;
    private PromptDialog promptDialog;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private String imageUrl;
    //0-未认证 1-已认证 2-认证中
    private int driveAuthStatus;
    private String currentImgLocalPath = "";
    /**
     * 入口
     *
     * @param mContext
     */
    public static void startAction(Context mContext, int type) {
        Intent intent = new Intent(mContext, DriverInformationActivity.class);
        intent.putExtra(DRIVE_AUTH_STATUS, type);
        mContext.startActivity(intent);
    }
    @Override
    protected int getContentView() {
        return R.layout.ac_driver_information;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, getString(R.string.top_bar_driver_information));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout), getString(R.string.dialogpic_notice),new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
    }

    @Override
    protected void initData() {
        driveAuthStatus  = getIntent().getIntExtra(DRIVE_AUTH_STATUS,0);
        imageLoader = ImageLoader.getInstance();
        options = MyApplication.getDisplayImageOptions(R.drawable.bg_default_driver);
        showView();
    }

    /**
     * 显示相应视图
     */
    private void showView(){
        switch (driveAuthStatus){
            case 0:
                layoutVerifyDriver.setVisibility(View.VISIBLE);
                layoutPromptSuccess.setVisibility(View.GONE);
                layoutPromptStatus.setVisibility(View.GONE);
                break;
            case 1:
                layoutVerifyDriver.setVisibility(View.GONE);
                layoutPromptSuccess.setVisibility(View.VISIBLE);
                layoutPromptStatus.setVisibility(View.GONE);
                //审核通过回显照片
                imageLoader.displayImage(DataCheckUtil.getMemberObj(mContext).driving_license_image.source,ivDriverSuccess,options);
                break;
            case 2:
                layoutVerifyDriver.setVisibility(View.GONE);
                layoutPromptSuccess.setVisibility(View.GONE);
                layoutPromptStatus.setVisibility(View.VISIBLE);
                break;
        }
    }
    @OnClick({R.id.tvDriverInfoTakePhoto, R.id.tvDriverInfoNext})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvDriverInfoTakePhoto:
                String ImageMessage="时间:"+ TimeUtil.getFormatTime()+"地点:"+""+"经纬度:";
                //拍摄驾驶证
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        TakePhotoActivity.DO_BIG_IMAGE, 2, 1, 640, 360), 101);

                break;
            case R.id.tvDriverInfoNext:
                //申请验证
                loadEditImage();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 101 && resultCode == TakePhotoActivity.UPLOAD_SUCCESS) {
            if(data != null && data.getExtras() != null) {
//                Uri avatarUri = data.getExtras().getParcelable("PATH");
//                assert avatarUri != null;
//                ivDriverInfo.setImageURI(avatarUri);
                currentImgLocalPath = data.getExtras().getString("PATH");
                MyApplication.getDefaultFinalBitmap(mContext).display(ivDriverInfo, currentImgLocalPath);
                tvDriverInfoNext.setBackgroundResource(R.drawable.bg_btn_green_shape_selector);
                tvDriverInfoTakePhoto.setText("请点击重新拍照");
            }
        }
    }

    private void loadEditImage() {
        if (StringUtils.isBlank(currentImgLocalPath)){
            ToastUtil.showShort(mContext,"请先选择照片");
            return;
        }
        new LHttpLib().editImage(mContext, memberId, Constants.IMAGE_TYPE_DRIVER_ID, currentImgLocalPath, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                Image image = new Image().parse(jsonStatus.data.optJSONObject("image_info"));
                updateImageInfo(image.source);

            }
        });
    }
    void updateImageInfo(String value) {
        new LHttpLib().editMemberInfo(mContext, memberId, Constants.IMAGE_TYPE_DRIVER_ID, value, "", new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                //上传图片成功之后 将 currentImgLocalPath支空
                currentImgLocalPath = "";
                //刷新数据
                DataLoadUtil.refreshMemberData(mContext);
                //进入下一个步骤
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        driveAuthStatus = 2;
                        showView();
                    }
                });
            }
        });
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
